package com.javashizhan.demo.restclient.client.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import org.springframework.web.client.RestTemplate;

import com.javashizhan.demo.restclient.client.core.RestClientPath;

/**
 * 
 * @Description: RestClient代理类
 *
 * @author 飞流
 * @date 2019年8月17日
 * @知识星球 Java实战营
 * @微信公众号 JAVA栈实战营
 * @站点 http://javashizhan.com/
 *
 */
public class RestClientProxy implements InvocationHandler {

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		// 获取接口类方法上远程地址
		RestClientPath path = method.getAnnotation(RestClientPath.class);
		String url = path.url();
		
		System.out.println("path.url(): " + url);
		
		RestTemplate restTemplate = new RestTemplate();
		// 这里arg[0]为方法参数
		Object object = restTemplate.postForEntity(url, args[0], method.getReturnType()).getBody();

		return object;
	}
}